<?php

require_once 'encrypt.inc.php';

class mySQL {

  var $db = array(
    "db_user" => "database_user",
    "db_pass" => "datbase_password",
    "db_host" => "localhost", // Shouldn't need changing
    "db_dbname" => "datbase_name",
  );
  var $selected_db;
  var $error = array();
  var $count = 0;
  var $dbHandle = "";
  var $query_id = "";

  function __construct($db) {
    $this->db = $db;
  }

  function dbconnect() {
    $Password = new Password();
    try {
      $this->dbHandle = @mysql_connect($this->db['db_host'], $this->db['db_user'], $Password->decrypt($this->db['db_pass']));
    } catch (Exception $e) {

      return FALSE;
    }

    return $this->dbHandle;
  }

  function connect() {
    $this->dbHandle = mysql_connect($this->db['db_host'], $this->db['db_user'], $this->db['db_pass'])
      OR $this->error(1, "Can't connect to mySQL Server. Username or password incorect");
  }

  function selected_db($db_name) {
    $this->selected_db = $dbname;
    mysql_select_db($db_name, $this->dbHandle)
      OR $this->error(1, "Can't select mySQL Database.");
  }

  function query($sql) {
    $this->query_id = mysql_query($sql, $this->dbHandle) OR $this->error(1, mysql_error());

    $this->count++;

    return $this->query_id;
  }

  function last_insert_id() {
    return mysql_insert_id($this->dbHandle);
  }

  function numrows($query) {
    $query = mysql_num_rows($query);

    return $query;
  }

  function getQueryResultArray($query_result) {
    $result_array = array();
    while ($row = mysql_fetch_array($query_result, MYSQLI_ASSOC)) {
      $result[] = $row;
    }
    return $result;
  }

  function getQueryResultColumns($query_result) {
    $columns = array();
    $row = mysql_fetch_assoc($query_result);
    foreach ($row as $colname => $v) {
      $columns[] = $colname;
    }
    return $columns;
  }

  function end() {
    mysql_close($this->dbHandle);
  }

  function error($error, $quote) {

    if ($error == 1) {

      die("An error has occurred : " . $quote);
    }

    return $quote;
  }

  function getDbTables() {
    return $this->getQueryResultArray(mysql_list_tables($this->db["db_dbname"]));
  }

  function getDatabases() {
    return $this->getQueryResultArray(mysql_list_dbs($this->dbHandle));
  }

  function getTableFields($table_name) {
    $query = "SHOW COLUMNS FROM `$table_name`";
    return $this->getQueryResultArray($this->query($query));
  }

  function getMysqlServerData() {
    $result = array();
    $result["Server"] = @mysql_get_host_info($this->dbHandle);
    $result["Protocol version"] = @mysql_get_proto_info($this->dbHandle);
    $result["Server version"] = @mysql_get_server_info($this->dbHandle);

    return $result;
  }

// Returns Server variables
  /*
   * Structure
   * Array[
   *       var_name [ "global"=>global_value
   *                  "sessiion"=>session_value
   *                ]
   *      ]
   */
  function getServerVariables() {
    $query = "SHOW GLOBAL VARIABLES";
    $global_v = $this->getQueryResultArray($this->query($query));
    $query = "SHOW SESSION VARIABLES";
    $session_v = $this->getQueryResultArray($this->query($query));

    $result = array();
    foreach ($global_v as $k => $v) {
      $result[$v["Variable_name"]]["global"] = $v["Value"];
      $result[$v["Variable_name"]]["session"] = $session_v[$k]["Value"];
    }

    return $result;
  }

  function getCharsets() {
    $sql = "SELECT * FROM information_schema.CHARACTER_SETS order by CHARACTER_SET_NAME asc";
    $charsets = $this->getQueryResultArray($this->query($sql));
    return $charsets;
  }

  function getCharsetCollation($charset) {
    $sql = "SELECT * FROM information_schema.COLLATIONS WHERE CHARACTER_SET_NAME= '" . mysql_real_escape_string($charset) . "' ORDER BY COLLATION_NAME ASC";
    $collations = $this->getQueryResultArray($this->query($sql));
    return $collations;
  }

  function getServerCharsets() {
    $s_charsets = array();
    $charsets = $this->getCharsets();
    //var_dump($charsets);
    foreach ($charsets as $k => $charset) {
      $c_collations = $this->getCharsetCollation($charset["CHARACTER_SET_NAME"]);
      $result[$charset["CHARACTER_SET_NAME"]]["description"] = $charset["DESCRIPTION"];
      $result[$charset["CHARACTER_SET_NAME"]]["collations"] = $c_collations;
    }
    return $result;
  }

}

?>